package com.lin.csdn.controller;

import com.lin.csdn.service.impl.CollectService;
import com.lin.csdn.utils.BlogJsonUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/collectServlet")
public class CollectServlet extends HttpServlet {
    /**
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     *
     * 0：获取收藏夹的博客数据 GetCollectBlog
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding ("UTF-8");
        resp.setContentType("application/json;charset=UTF-8");
        //建立功能代表的code调用
        String GetCollectBlog="0";
        //获取req数据
        String code=req.getParameter("code");
        String userName=req.getParameter("userName");
        List<Object> data = null;

        CollectService collectService=new CollectService();
        BlogJsonUtil jsonUtil=new BlogJsonUtil();

        //根据需求调用各自的方法
        if(code.equals(GetCollectBlog)){
            data=collectService.getCollectBlog(userName);
        }
        PrintWriter writer = resp.getWriter(); //响应流
        if(data==null){
            JSONObject object=new JSONObject();
            writer.print(object);
        }
        else {
            JSONArray array = jsonUtil.blogJson(data);
            writer.print(array);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
